/***********************************************
 **** 版权声明处 **
 ****  为了方便阅读和维护，请严格遵守相关代码规范，谢谢   ****
 *******************************************/
package utils
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
	import mx.core.Container;
	import mx.core.IVisualElement;
	import mx.core.IVisualElementContainer;
	import mx.core.UIComponent;
	
	import spark.components.Group;

	/*******************************************
	 **** @author huangjixin,@date 2012-6-26, @time 上午9:21:38 **
	 **** 水印工具类  **
	 *******************************************/
	public class LicenseHandler
	{
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// private 类私有静态变量和静态常量声明处。（全部大写，使用下划线进行分割）
		// 例如：private static const EXAMPLE:String = "example";
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		private static const dBuild:Number = new Date().time;
		private static const TRIAL_PERIOD_DAYS:Number = DAY * 30;	//一个小时；
		private static const DAY:Number = 86400000; //一天；
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// public 类公有静态变量和静态常量声明处。（全部大写，使用下划线进行分割）
		// 例如：public static const EXAMPLE:String = "example";
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// private 私有变量声明处，请以“_”开头定义变量
		// 例如：private var _example:String;
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		private var _displayObjectContainer:UIComponent;
		private var _darkBackground:Boolean;
		private var _time:Number;
		
		private var _textField:TextField;
		
		private var _foreground:Sprite;
		
		private var _textFieldContainer:UIComponent;
		
		private var timer:Timer;
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// public 公有变量声明处
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// 构造函数，初始化相关工作可以放在里面
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

		public function LicenseHandler(displayObjectContainer:UIComponent, darkBackground:Boolean,time:Number)
		{
			this._displayObjectContainer = displayObjectContainer;
			this._time = time;
			this._darkBackground = darkBackground;
			this._displayObjectContainer.addEventListener(Event.ENTER_FRAME, this.enterFrameHandler);
			
			timer = new Timer(5000);
			timer.start();
			timer.addEventListener(TimerEvent.TIMER,onTimer)
		}//构造函数结束
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// getter和setter函数
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// 相关事件响应函数和逻辑函数存放处
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		protected function onTimer(event:TimerEvent):void{
			this._time = new Date().time;
		}
		
		protected function checkVisible(param1:DisplayObjectContainer) : Boolean
		{
			return true;
		}// end function
		
		private function enterFrameHandler(event:Event):void
		{
			var _graphics:Graphics = null;
			var _uiComponent:UIComponent = null;
			var _textFormat:TextFormat = null;
			var _str:String = null;
			var _array:Array = null;
			var _uiComponent2:UIComponent = null;
			
			if (this._textField != null && !this.checkVisible(this._displayObjectContainer))
			{
				this._textField.visible = false;
				return;
			}
			
			var _point1:Point = new Point();
			var _point2:Point = new Point(this._displayObjectContainer.width, this._displayObjectContainer.height);
			
			_point1 = this._displayObjectContainer.localToGlobal(_point1);
			_point2 = this._displayObjectContainer.localToGlobal(_point2);
			var _wid:Number = Math.abs(_point2.x - _point1.x);
			var _hei:Number = Math.abs(_point2.y - _point1.y);
			
			if (this._time > dBuild + TRIAL_PERIOD_DAYS)
			{
				if (!this._foreground)
				{
					this._foreground = new Sprite();
					this._foreground.mouseEnabled = false;
				}
				if (!this._foreground.parent)
				{
					_uiComponent = new UIComponent();
					if(this._displayObjectContainer is IVisualElementContainer){
						IVisualElementContainer(this._displayObjectContainer).addElement(_uiComponent);
					}else{
						this._displayObjectContainer.addChild(_uiComponent);
					}
					
					_uiComponent.addChild(this._foreground);
				}
				this._foreground.width = this._displayObjectContainer.width;
				this._foreground.height = this._displayObjectContainer.height;
				_graphics = this._foreground.graphics;
				_graphics.clear();
				_graphics.beginFill(1118481,0.5);//, (this._time - dBuild - TRIAL_PERIOD_DAYS) / (1 * DAY) 根据日期不同会有不同的透明度；
				_graphics.drawRect(0, 0, this._displayObjectContainer.width, this._displayObjectContainer.height);
				_graphics.endFill();
			}
			
			if (_wid < 20 && _hei < 20 || !this._displayObjectContainer.visible)
			{
				if (this._textField != null)
				{
					this._textField.visible = false;
				}
				return;
			}
			
			if (!this._textField)
			{
				this._textField = new TextField();
				this._textField.selectable = false;
				this._textField.autoSize = TextFieldAutoSize.CENTER;
				this._textField.textColor = 16777215;
				this._textField.backgroundColor = 0;
				_textFormat = new TextFormat();
				_textFormat.font = "Verdana";
				_textFormat.size = 32;
				_textFormat.color = 0x0;
				_textFormat.bold = true;
				this._textField.defaultTextFormat = _textFormat;
				this._textField.alpha = 1;
				this._textField.mouseEnabled = false;
				_array = [];
//				_array.push(new GlowFilter(0x696969, 1, 6, 6, 2, 1, false, true));
				_array.push(new GlowFilter(this._darkBackground ? (16777215) : (0x696969), 1, 6, 6, 2, 1, false, true));
				this._textField.filters = _array;
				this._textField.x = Math.round(-10 * Math.random());
				this._textField.y = Math.round(-40 * Math.random());
				this._textField.visible = true;
			}
			
			if (_str == null || _str.length < 1)
			{
				_str = "BPM编辑器软件";
			}
			if (this._time < dBuild + TRIAL_PERIOD_DAYS)
			{
//				_str += (" "+"距离结束还有" + Math.round((dBuild + TRIAL_PERIOD_DAYS - this._time) / DAY) + "天");	
			}else{
				_str += "结束";
			}
//			_str = _str + ("距离结束还有" + Math.round((dBuild + TRIAL_PERIOD_DAYS - this._time) / DAY) + "天");
			
			this._textField.text = _str;
			
			if (!this._textField.parent)
			{
				_uiComponent2 = new UIComponent();
				if(this._displayObjectContainer is IVisualElementContainer){
					IVisualElementContainer(this._displayObjectContainer).addElement(_uiComponent2);
				}else{
					this._displayObjectContainer.addChild(_uiComponent2);
				}
				_uiComponent2.addChild(this._textField);
				_uiComponent2.width = this._displayObjectContainer.width;
				_uiComponent2.height = this._displayObjectContainer.height;
			}
			if (!this._textField.visible)
			{
				this._textField.visible = true;
			}
			
			this._textField.x = this._displayObjectContainer.width / 2 - this._textField.width / 2;
			this._textField.y = this._displayObjectContainer.height / 2 - this._textField.height / 2;
			this._textField.visible = true;//_textField应该那个水印的组件，设置为可见即可。
			return;
		}//enterFrameHandler结束
		/**
		 * 对容器打水印 
		 * @param param1
		 * @param param2
		 * 
		 */
		public static function displayWatermark(displayObjectContainer:UIComponent, darkBackground:Boolean = false) : void
		{
			new LicenseHandler(displayObjectContainer, darkBackground, new Date().time);
			return;
		}// end function
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// override 覆盖函数
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
	}//类结束
}//包结束